
<div class="clear" style="margin-top:20px;"></div>

<div class="login_phone">
	<form action="" method="post" id="sendForm">
		<input type="text" id="tel" name="phone" style="background-color:white;border-radius:5px;" value="<?php echo $this->phone;?>" class="text_type" placeholder="请输入手机号码"/>
		<input type="hidden" name="type" value="send"/>
		<input type="hidden" name="rurl" value="<?php echo $this->rurl;?>"/>
		<input type="button" style="border:0px;border-radius:5px;" class="butt_sty" id="btn" value="获取验证码" onclick="javascript:submitForm(this);return false;" />

	</form>
</div>
<div class="clear"></div>
<form action="" method="">
	<div class="yanz_sty">
		<input type="text" name="captcha" id="captcha" style="background-color:white;border-radius:5px;" class="text_type2"  placeholder="请输入短信中的验证码"/>
	</div>
	<div id="verification" class="login_phone" style="margin-top:20px;display:none;border-radius:5px;">
		<input type="text" id="code" name="verification" placeholder="请输入右边验证码" class="text_type">
		<img src="<?php echo $this->captchaId;?>" onclick="javascript:changeCaptcha(this);return false;">
	</div>
	<div class="login_sty">
		<input type="hidden" name="phone" id="yz_phone" value="<?php echo $this->phone;?>"/>
		<input type="hidden" id="tag" name="type" value="login"/>
		<input type="hidden" id="verify" name="verify" value="2"/>
		<input type="hidden" id="rurl" name="rurl" value="<?php echo $this->rurl;?>"/>
		<input type="button" style="border-radius:5px;" class="butt_sty2" id="LoginBtn" onclick="javascript:toLogin();return false;"  value="验证并登录"/>
	</div>
</form>
<div class="clear"></div>
<div id="error" style="text-align:center;font-size;0.9em;color:red;margin-top:10px;"><?php if($this->errorMsg) echo $this->errorMsg;?></div>
<div style="text-align:center;font-size:0.9em;color:#555;margin-top:20px;">若未收到验证码，请拨打<a href="tel://021-54573620">021-54573620</a></div>
<script type="text/javascript">
	$(document).ready(function(){
		$('#tel').blur(function(){
			$('#yz_phone').attr('value',$("#tel").val());
		});
	});
	var countdown=60;
	function settime(el) {
		if (countdown == 0) {
			el.removeAttribute("disabled");
			el.value="获取验证码";
			countdown = 60;
			return;
		} else {
			el.setAttribute("disabled", true);
			el.value="重新发送(" + countdown + ")";
			countdown--;
		}
		setTimeout(function() { settime(el) },1000)
	}
	function submitForm(el){
		$('#error').html('');
		var codeDisplay = $('#verification').css('display');
		if(codeDisplay!='none'){
			$('#error').html('请勿重复获取验证码');
			return false;
		}
		var phone = Trim($("#tel").val(),'g');
        if(phone.length==0)
        {
           $('#error').html('请输入手机号码！');
           el.focus();
           return false;
        }
        if(phone.length!=11)
        {
            $('#error').html('请输入有效的手机号码！');
            el.focus();
            return false;
        }

        var myreg = /^1[3|4|5|7|8][0-9]\d{4,8}$/;
        if(!myreg.test(phone))
        {
            $('#error').html('请输入有效的手机号码！');
            $("#tel").focus();
			return false;
        }
		settime(el);
		$('#yz_phone').attr('value',phone);
		$.ajax({
		  type: "post",
		  url : "/login",
		  dataType:'json',
		  data:{'phone':phone,'type':'send'},
		  success: function(info){
			var msg = info.msg;
			if(msg =="phoneError"){
				$('#error').html('请输入有效的手机号码！');
				return;
			}
			if(msg=="getFailed"){
				$('#error').html('获取验证码失败');
				return;
			}
			var amount = parseInt(info.amount);
			if(amount>2){
				$('#verification').show();
				$('#verify').val(1);
			}else{
				$('#verification').hide();
				$('#verify').val(2);
			}
		  }
		});
	}
	function changeCaptcha(el){
		$.ajax({
		  type: "get",
		  url : "/login",
		  dataType:'json',
		  data:{'tag':'changeCaptcha'},
		  success: function(info){
			var captcha = info.msg;
			$(el).attr('src',captcha);
		  }
		});
	}

	function toLogin(){
		var phone = $('#yz_phone').val();
		var captcha = $('#captcha').val();
		var code = '';
		var verify = $('#verify').val();
		var rurl = $('#rurl').val();
		 $('#error').html('');
		if(isNull(phone)){
			$('#error').html('请获取验证码');
			return false;
		}
		 if(phone.length!=11)
        {
            $('#error').html('请输入有效的手机号码！');
            return false;
        }

        var myreg = /^1[3|4|5|7|8][0-9]\d{4,8}$/;
        if(!myreg.test(phone))
        {
            $('#error').html('请输入有效的手机号码！');
			return false;
        }
		if(isNull(captcha)){
			$('#error').html('请输入验证码');
			return false;
		}
		if(verify == 1){
			code = $('#code').val();
			if(isNull(code)){
				$('#error').html('请输入右边的校验码');
				return false;
			}
		}
		// alert(phone);
		// alert(captcha);
		// alert(code);
		// alert(verify);
		// alert(rurl);
		// return false;
		$('#LoginBtn').attr("disabled","true");
		$('#LoginBtn').css('background-color','grey');
		$.ajax({
		  type: "post",
		  url : "/login",
		  dataType:'json',
		  data:{'tpye':'toLogin','phone':phone,'captcha':captcha,'code':code,'verify':verify,'rurl':rurl},
		  success: function(info){
		    var msg = info.msg;
			if(msg=='paraNull'){
				$('#error').html('请填写手机号和验证码');
				return false;
			}else if(msg=='captchaWrong'){
				$('#error').html('验证码错误');
				return false;
			}else if(msg=='codeWrong'){
				$('#error').html('校验码错误');
				return false;
			}else if(msg == 'success'){
				var rurl = info.rurl;
				if(isNull(rurl)){
					rurl = '/';
				}
				location.href=rurl;
			}else{
				
			}
		  },
		  complete: function () {
			  setTimeout(function(){
				$('#LoginBtn').removeAttr("disabled");
				$('#LoginBtn').css('background-color','#2EA821');
			  },4000);
		  }
		});
	}

</script>